/*
 * Copyright 2003-2005 (c) Live Media Pty Ltd. <argot@einet.com.au> 
 *
 * This software is licensed under the Argot Public License 
 * which may be found in the file LICENSE distributed 
 * with this software.
 *
 * More information about this license can be found at
 * http://www.einet.com.au/License
 * 
 * The Developer of this software is Live Media Pty Ltd,
 * PO Box 4591, Melbourne 3001, Australia.  The license is subject 
 * to the law of Victoria, Australia, and subject to exclusive 
 * jurisdiction of the Victorian courts.
 */

using System;

namespace argot
{
	
	/// <summary>
	/// This reads objects directly off the stream.
	/// </summary>
	public class TypeInputStream
	{
		private TypeMap _map;
		private System.IO.Stream _in;
		
		public TypeInputStream(System.IO.Stream inStream, TypeMap map)
		{
			_in = inStream;
			_map = map;
		}

		virtual public TypeMap TypeMap
		{
			get
			{
				return _map;
			}
			
			set
			{
				_map = value;
			}
			
		}
		
		public System.IO.Stream Stream
		{
			get
			{
				return _in;
			}
		}

		public virtual System.Object ReadObject(int id)
		{
			TypeReader reader = _map.GetReader(id);
			TypeElement element = _map.GetStructure(id);
			return reader.Read(this, element);
		}
		
		public virtual System.Object ReadObject(System.String name)
		{
			int id = _map.GetId(name);
			if (id == TypeMap.NOTYPE)
				throw new TypeException("type not registered");
			
			TypeReader reader = _map.GetReader(id);
			TypeElement element = _map.GetStructure(id);
			return reader.Read(this, element);
		}
	}	
}